const config = require('../config/config');
const dataUrl = config.testServer;
const FormData = require('form-data')
const axios = require("axios");
const qs = require('qs');
module.exports = {
    getAllAvaliableConfigs,
    initProjectConfigs,
    getEventsWithTypeByDuration,
    initNewConfiguration,
    getProjectConfigsById,
    getReprtsWithTypeByDuration,
    getLiveSteramByDeviceType,
    getHomePageDevices,
    getProjectManagementInfos,
    getThumbnailByType,
    getProgressWithType,
    getBasicInfoByDuration,
    getSiteNamePlate,
    getLeanInfoByDuration,
    getHomePageDevicesImpl,
    getChartsInfoByDuration,
    getSafetyViolationByDuration,
    getLeanViolationByDuration,
    getBuildingProgressInfo,
    getBuildingProgressInfoImpl,
    getProgressByDuration,
    getReportList,
    getReportById,
    deleteViolationData,
    getVehicleDataByDuration,
    addLprWhiteList,
    getLprWhiteList,
    removeLprWhiteList,
    getLiveStreamUrl,
    updateLprInfosById,
    getRobotInfos,
    uploadFileToOSS,
    downloadFile,
    getAllFileList,
    addNewDirPath,
    getPersonInfoDetailById,
    getDailyStatistic,
    getDailyAttendence,
    getPersonInfoList,
    getWorkingAreaPersonNumData
}

async function getLiveStreamUrl(payload) {
    let url = dataUrl + "api/v1/data/getLiveStreamUrl"
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/json'
        },
        data: payload
    }

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getAllAvaliableConfigs() {
    let url = dataUrl + "api/v1/common/getCurrentProjectInfo"
    let data = qs.stringify({

    });

    let config = {
        method: 'get',
        maxBodyLength: Infinity,
        url: url,
        headers: {},
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }

}


async function initProjectConfigs(payload) {
    let url = dataUrl + "api/v1/common/initProject"
    let data = payload
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/json'
        },
        data: data
    };

    await axios.request(config)
        .then((response) => {
            return JSON.stringify(response.data)
        })
        .catch((error) => {
            return error
        });

}

async function initNewConfiguration(payload) {
    let url = dataUrl + "api/v1/common/initNewConfiguration?unionId" + payload.unionId
    let data = payload
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/json'
        },
        data: data
    };

    await axios.request(config)
        .then((response) => {
            return JSON.stringify(response.data)
        })
        .catch((error) => {
            return error
        });
}

async function getEventsWithTypeByDuration(payload, type) {
    console.log(payload, type)
    let url, data;
    if (type === 'lean_history') {
        url = dataUrl + "api/v1/data/getEventsWithTypeByDurationImpl"
        data = qs.stringify({
            startDate: payload.startDate,
            endDate: payload.endDate,
            deviceType: payload.algorithmType,
            unionId: payload.unionId,
            algorithmType: "lean_origin"
        })
    }
    else if (type === 'lean_violation') {
        url = dataUrl + "api/v1/data/getEventsWithTypeByDurationImpl"
        data = qs.stringify({
            startDate: payload.startDate,
            endDate: payload.endDate,
            deviceType: payload.algorithmType,
            unionId: payload.unionId,
            algorithmType: "lean_violation"
        })

    }
    else if (type === 'safety_violation') {
        console.log(payload)
        url = dataUrl + "api/v1/data/getEventsWithTypeByDurationImpl"
        data = qs.stringify({
            startDate: payload.startDate,
            endDate: payload.endDate,
            deviceType: payload.algorithmType,
            unionId: payload.unionId,
            algorithmType: payload.algorithmType,
            zone: payload.algorithmType
        })

    }
    else if (type === 'ppl_count') {
        console.log(payload)
        url = dataUrl + "api/v1/data/getEventsWithTypeByDurationImpl"
        data = qs.stringify({
            startDate: payload.startDate,
            endDate: payload.endDate,
            deviceType: payload.algorithmType,
            unionId: payload.unionId,
            algorithmType: "ppl_count"
        })
    }
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}




async function getReprtsWithTypeByDuration(payload) {
    let url = dataUrl + "api/v1/data/getReportsWithTypeByDuration"
    let data = qs.stringify({
        startDate: payload.startDate,
        endDate: payload.endDate,
        unionId: payload.unionId,
        reportType: payload.reportType
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getProjectConfigsById(payload) {
    let url = dataUrl + "api/v1/common/getProjectConfigsById"
    let data = payload
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    await axios.request(config)
        .then((response) => {
            console.log(JSON.stringify(response.data));
            return JSON.stringify(response.data)
        })
        .catch((error) => {
            return error
        });
}

async function getLiveSteramByDeviceType(payload) {
    let url = dataUrl + "api/v1/data/getLiveSteramByDeviceType"
    let data = payload
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    await axios.request(config)
        .then((response) => {
            console.log(JSON.stringify(response.data));
            return JSON.stringify(response.data)
        })
        .catch((error) => {
            return error
        });
}


async function getHomePageDevices(payload) {
    let url = dataUrl + "api/v1/common/getHomePageDevices"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        infoType: payload.infoType
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getProjectManagementInfos(payload) {
    let url = dataUrl + "api/v1/common/getProjectManagementInfos"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        infoTypes: payload.infoTypes
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}


async function getThumbnailByType(payload) {
    let url = dataUrl + "api/v1/data/getThumbnailByType"
    let data = qs.stringify({
        unionId: payload.unionId,
        algorithmType: payload.algorithmType
    });
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getProgressWithType(payload) {
    let url = dataUrl + "api/v1/data/getProgressWithType"
    let data = qs.stringify({
        unionId: payload.unionId,
        progressType: payload.progressType
    });
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getBasicInfoByDuration(payload, type) {
    let url = dataUrl + "api/v1/data/getBasicInfoByDuration"
    let data = qs.stringify({
        startDate: payload.startDate,
        endDate: payload.endDate,
        unionId: payload.unionId,
        infoType: type,
        algorithmType: payload.algorithmType
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}


async function getSiteNamePlate(payload) {
    console.log(payload.unionId)
    let url = dataUrl + "api/v1/common/getSiteNamePlate"
    let data = qs.stringify({
        unionId: payload.unionId,
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }

}


async function getLeanInfoByDuration(payload) {

}

async function getHomePageDevicesImpl(payload) {
    let url = dataUrl + "api/v1/common/getHomePageDevicesImpl"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getChartsInfoByDuration(payload) {
    let url = dataUrl + "api/v1/data/getChartsInfoByDuration"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}


async function getSafetyViolationByDuration(payload) {
    let url = dataUrl + "api/v1/data/getSafetyViolationByDuration"
    const qs = require('qs');
    let data = qs.stringify(payload);
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getLeanViolationByDuration(payload) {
    let url = dataUrl + "api/v1/data/getLeanViolationByDuration"
    const qs = require('qs');
    let data = qs.stringify(payload);

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getBuildingProgressInfo(payload) {
    let url = dataUrl + "api/v1/data/getBuildingProgressInfo"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getBuildingProgressInfoImpl(payload) {
    let url = dataUrl + "api/v1/data/getBuildingProgressInfo"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getProgressByDuration(payload) {
    console.log(payload)
    let url = dataUrl + "api/v1/data/getBuildingProgressInfoImpl"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getReportList(payload) {
    let url = dataUrl + "api/v1/data/getReportList"
    const qs = require('qs');
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    });
    console.log(data)
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getReportById(payload) {
    let url = dataUrl + "api/v1/data/getReportById"
    const qs = require('qs');
    // console.log("here?")
    let data = qs.stringify({
        unionId: payload.unionId,
        reportId: payload.reportId
    });

    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function deleteViolationData(payload, type) {
    let url = dataUrl + "api/v1/data/deleteViolationData"
    console.log(payload, type)
    // let newData = {
    //     unionId: payload.unionId,
    //     type: type,
    //     urlList: payload.dataList
    // }
    let data = qs.stringify({
        unionId: payload.unionId,
        type: type,
        urlList: payload.urlList
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };

    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getVehicleDataByDuration(payload) {
    console.log("fuck")
    let url = dataUrl + 'api/v1/data/getEventsWithTypeByDuration'
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate,
        algorithmType: payload.algorithmType
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}


async function getLprWhiteList(payload) {
    let url = dataUrl + 'api/v1/data/getLprWhiteList'
    let data = qs.stringify({
        unionId: payload.unionId,
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}

async function addLprWhiteList(payload) {
    let url = dataUrl + 'api/v1/data/addLprWhiteList'
    let data = qs.stringify({
        unionId: payload.unionId,
        lprNumber: payload.lprNumber,
        validStartTime: payload.validStartTime,
        validEndTime: payload.validEndTime,
        notifyGroup: payload.notifyGroup
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}

async function updateLprInfosById(payload) {
    const vehicleTypeTranslations = {
        "汽车吊": "crane",
        "装载车": "loader",
        "泵车": "pump truck",
        "搅拌车": "concrete mixer",
        "汽车": "car",
        "巴士": "bus",
        "卡车": "truck"
    };

    let vehicleType = vehicleTypeTranslations[payload.vehicleType] || "other vehicle";

    let url = dataUrl + 'api/v1/data/updateLprInfosById'
    let data = qs.stringify({
        unionId: payload.unionId,
        lprNumber: payload.lprNumber,
        id: payload.id,
        vehicleType: vehicleType,
        direction: payload.direction
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}

async function removeLprWhiteList(payload) {
    let url = dataUrl + 'api/v1/data/removeLprWhiteList'
    let data = qs.stringify({
        unionId: payload.unionId,
        lprNumber: payload.lprNumber
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}

async function getRobotInfos(payload) {
    let url = dataUrl + 'api/v1/data/getRobotInfos'
    let data = qs.stringify({
        unionId: payload.unionId,
        lprNumber: payload.lprNumber
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        // console.log(result.data)
        return result.data
    } catch (error) {
        return error
    }
}

async function uploadFileToOSS(req, res, form) {
    let response = await axios({
        method: 'post',
        url: dataUrl + 'api/v1/data/uploadFileToOSS',
        data: form,
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        maxContentLength: 1024 * 1024 * 1024,
        maxBodyLength: 1024 * 1024 * 1024
    })
    res.json({
        response: response.data
    })
}

async function downloadFile(payload) {
    let url = dataUrl + 'api/v1/data/downloadFileFromOSS'
    let data = qs.stringify({
        unionId: payload.unionId,
        filePath: payload.filePath
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        responseType: 'stream',
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getAllFileList(payload) {
    let url = dataUrl + 'api/v1/data/getAllFileList'
    let data = qs.stringify({
        unionId: payload.unionId,
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function addNewDirPath(payload) {
    let url = dataUrl + 'api/v1/data/addNewDirPath'
    let data = qs.stringify({
        unionId: payload.unionId,
        dirTag:payload.dirTag,
        dirName:payload.dirName
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getPersonInfoDetailById(payload) {
    let url = dataUrl + 'api/v1/data/getPersonInfoDetailById'
    let data = qs.stringify({
        unionId: payload.unionId,
        idCardNo: payload.idCardNo
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getDailyStatistic(payload) {
    let url = dataUrl + 'api/v1/data/getDailyStatistic'
    let data = qs.stringify({
        unionId: payload.unionId,
        date: payload.date
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getDailyAttendence(payload) {
    let url = dataUrl + 'api/v1/data/getDailyAttendence'
    let data = qs.stringify({
        unionId: payload.unionId,
        startTime: payload.startTime,
        endTime: payload.endTime
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getPersonInfoList(payload) {
    let url = dataUrl + 'api/v1/data/getPersonInfoList'
    let data = qs.stringify({
        unionId: payload.unionId,
        pageIndex: payload.pageIndex,
        searchText: payload.searchText ? payload.searchText : ""
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

async function getWorkingAreaPersonNumData(payload) {
    let url = dataUrl + 'api/v1/data/getWorkingAreaPersonNumData'
    let data = qs.stringify({
        unionId: payload.unionId,
        startDate: payload.startDate,
        endDate: payload.endDate
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}